/*********************************************************************
 *                                                                   *
 *  Js Css Minifier ( JsCssMin )                                     *
 *  Author: Igor Kanshyn                                             *
 *  Copyright (c) 2009, Igor Kanshyn. All rights reserved.           *
 *                                                                   *
 *  JsCssMin is released under the Apache Software License.          *
 *  See LICENSE.txt for more details.                                *
 *                                                                   *
 *********************************************************************/

package org.grenader.jscssmin.files;

import static junit.framework.Assert.*;
import org.grenader.jscssmin.model.CSSResource;
import org.grenader.jscssmin.model.CSSResourceGroup;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.util.NoSuchElementException;

/**
 * <p>Product: JsCssMin ( JavaScript and CSS Minifier )<br>
 * Author: Igor Kanshyn (grenader).
 * </p>
 * Date: May 5, 2009
 */
public class ResourceTmpFileIteratorTestCase {
    private CSSResourceGroup group;

    @Before
    public void sutUp() {
        group = new CSSResourceGroup();
        CSSResource resource1 = new CSSResource("<link rel=\"stylesheet\" hREf=\"my.css\" type=\"text/css\" />");
        resource1.setTmpFile("file1");

        CSSResource resource2 = new CSSResource("<link rel=\"stylesheet\" hREf=\"my.css\" type=\"text/css\" />");
        resource2.setTmpFile("file2");

        group.addResource(resource1);
        group.addResource(resource2);
    }


    @Test
    public void testHasNextAndNext() {
        ResourceTmpFileIterator iterator = new ResourceTmpFileIterator(group);
        assertTrue(iterator.hasNext());
        File next1 = iterator.next();
        assertNotNull(next1);
        assertTrue(next1.getAbsolutePath().endsWith("file1"));

        assertTrue(iterator.hasNext());
        File next2 = iterator.next();
        assertNotNull(next2);
        assertTrue(next2.getAbsolutePath().endsWith("file2"));

        assertFalse(iterator.hasNext());
    }

    @Test(expected = NoSuchElementException.class)
    public void testNext_Error() {
        ResourceTmpFileIterator iterator = new ResourceTmpFileIterator(group);
        assertTrue(iterator.hasNext());
        assertNotNull(iterator.next());
        assertNotNull(iterator.next());

        iterator.next(); // throws an exeption
    }

    @Test(expected = NoSuchElementException.class)
    public void testNext_noTmpFile() {
        ResourceTmpFileIterator iterator = new ResourceTmpFileIterator(group);
        group.getResources().get(0).setTmpFile(null);

        assertTrue(iterator.hasNext());

        iterator.next(); // throws an exeption
    }


    @Test(expected = IllegalAccessError.class)
    public void testRemove() {
        ResourceTmpFileIterator iterator = new ResourceTmpFileIterator(group);
        iterator.remove();

    }
}

